package com.bdqn.systems.service.impl;

import com.alibaba.excel.util.CollectionUtils;
import com.bdqn.systems.converter.MenuConverter;
import com.bdqn.systems.converter.MenuTreeBuilder;
import com.bdqn.systems.entity.Menu;
import com.bdqn.systems.mapper.MenuMapper;
import com.bdqn.systems.service.MenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bdqn.systems.vo.MenuNodeVO;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author cp
 * @since 2020-11-30
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    /**
     * 加载菜单树
     * @return
     */
    @Override
    public List<MenuNodeVO> findMenuTree() {

        List<Menu> menus = this.baseMapper.selectList(null);
        //通过菜单转换进行数据转换
        List<MenuNodeVO> menuNodeVOS = MenuConverter.converterToMenuNodeVO(menus);
        //菜单树生成器建立多级菜单
        return MenuTreeBuilder.build(menuNodeVOS);
    }

    /**
     * 获取展开项
     * @return
     */
    @Override
    public List<Long> findOpenIds() {
        List<Long> ids = new ArrayList<>();

        List<Menu> menus = this.baseMapper.selectList(null);
        //集合不为空
        if(!CollectionUtils.isEmpty(menus)){
            for (Menu menu : menus) {
                //获取展开的id编号
                if(menu.getOpen()==1){
                    ids.add(menu.getId());
                }
            }
        }
        return ids;
    }
}
